// import { InjectRedis } from '@nestjs-modules/ioredis';
import { Controller, Get, Query, Version, UseInterceptors, Inject } from '@nestjs/common';
// import Redis from 'ioredis';
import { CACHE_MANAGER, CacheInterceptor } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { MailerService } from '@nestjs-modules/mailer';
import { PrismaService } from './database/prisma/prisma.service';
// import { InjectRepository } from '@nestjs/typeorm';
import { User } from './database/user/user.entity';
import { Repository } from 'typeorm';
import { UserRepository } from './user/user.repository';
let a = 1;
@Controller()
@UseInterceptors(CacheInterceptor)
export class AppController {
    private userRepository: Repository<User>;
    constructor(
        /*@InjectRedis() private readonly redis: Redis*/ @Inject(CACHE_MANAGER) private cacheManager: Cache,
        private readonly mailerService: MailerService,
        private readonly prismaService: PrismaService,
        private repository: UserRepository,

        // @InjectRepository(User)
        // private userRepository: Repository<User>,
        // @InjectRepository(User, 'mysql2')
        // private userRepository1: Repository<User>,
    ) {
        this.userRepository = repository.getRepository();
    }

    @Get('sql')
    async getSql(): Promise<any> {
        const users = await this.prismaService.user.findMany({});
        return users;
    }
    @Get('typesql')
    async getTypeSql(): Promise<any> {
        // const userRepository = this.userRepository.getRepository();
        // const users = await this.userRepository.find();
        // return users;
        const users = await this.userRepository.find();
        return users;
    }
    @Get('typesq2')
    async getTypeSql2(): Promise<any> {
        // const users = await this.userRepository1.find();
        // return users;
    }
    @Get()
    @Version('1')
    async getHello(@Query('token') token: string): Promise<any> {
        // const res = await this.redis.get('tst');
        // await this.redis.set('tst', token || 'default', 'EX', 600);
        // return 'Hello World! token is ' + res;
        const tt = await this.cacheManager.get('token');
        console.log(tt);
        await this.cacheManager.set('token', token || 'default111');
        return { token: tt };
    }
    @Get()
    @Version('2')
    getHelloV2(): number {
        a = a + 1;
        return a;
        // return 'hello2';
    }

    @Get('sendmail')
    @Version('1')
    sendMail() {
        this.mailerService
            .sendMail({
                to: '1240172019@qq.com',
                from: 'wudanpingdingshang@foxmail.com',
                subject: 'Testing Nest Mailermodule with template ✔',
                template: 'welcome',
                context: {
                    name: 'dandan',
                },
            })
            .then(() => {
                console.log('send success');
            })
            .catch(() => {});
    }
}
